<template>
	<view class="container">
		<view class="item-view">
			<children-base-top-view
				:name="assessDetailEntity.childrenName"
				:month="assessDetailEntity.month"
				:gender="assessDetailEntity.gender"
				:height="assessDetailEntity.height"
				:weight="assessDetailEntity.weight"
				:header="assessDetailEntity.header"
				:imgUrl="assessDetailEntity.children ? assessDetailEntity.children.imgUrl:''"
				:more="false"
			/>
		</view>
		
		<assess-bmi-view v-if="hasData" 
			:bmi="assessDetailEntity.bmi" 
			:name="assessDetailEntity.childrenName"
			:gender="assessDetailEntity.gender"
			:height="assessDetailEntity.height"
			:weight="assessDetailEntity.weight"
			:birthday="formatTimestamp(assessDetailEntity.birthday, 'yyyy-mm-dd')"/>
			
		<view class="item-view">
			<assess-scores-view v-if="hasData"
				:dimensionScores="assessDetailEntity.dimensionScores" 
				:wisdomAge="wisdomAge" 
				:dqFraction="dqFraction" 
				:level="level"/>
		</view>
		
		<assess-scores-chart v-if="hasData"
			:evaluatingDate="assessDetailEntity.evaluatingDate" 
			:dimensionScores="assessDetailEntity.dimensionScores"/>
			
		<view class="item-view item-view-grey">
			<assess-high-level v-if="hasData"
							   :paid="paid"
							   :high-level-info="assessDetailEntity.highLevelInfo"
							   :answer-sheet="assessDetailEntity.answerSheet"/>
		</view>
	</view>
</template>

<script>
	import ChildrenBaseTopView from '@/components/children-base-top-view.vue'
	import AssessScoresView from '../components/assess-scores-view.vue'
	import AssessHighLevel from '../components/assess-high-level.vue'
	import AssessBmiView from '../components/assess-bmi-view.vue'
	import AssessScoresChart from '../components/assess-scores-chart.vue'
	import { evlPsycalDetail, getAssessDetail } from '@/api/evaluate.js'
	import appMixin from '@/mixins/app.js'
	export default {
		mixins: [appMixin],
		components:{
			ChildrenBaseTopView,
			AssessScoresView,
			AssessHighLevel,
			AssessBmiView,
			AssessScoresChart
		},
		data() {
			return {
				hasData: false,

				assessDetailEntity: {
					dimensionScores: [],
					answerSheet: [],
					parentGranted: true,
				},

				wisdomAge: '',
				dqFraction: 0,
				level: '',
				suggest: '',
				warningConclusion: '',
				normalConclusion: '',
				
				///是否购买
				paid: false,
			}
		},
		onLoad(options) {
			this.evaluateRecordId = options.evaluateRecordId;
			this.getEvlPsycalDetail();
		},
		methods: {
			getEvlPsycalDetail() {
				if (this.appType === 'app-tch' || this.appType === 'mp-tch') {
					getAssessDetail(this.evaluateRecordId).then(res => {
						console.log(res);
						this.handleData(res.data);
					})
				} else if (this.appType === 'app-p' || this.appType === 'mp-p') {
					evlPsycalDetail(this.evaluateRecordId).then(res => {
						console.log(res);
						this.handleData(res.data);
					}) 
				}
			},
			
			handleData(data) {
				let month = data.month;
				
				let dqTotal = 0.0;
				let monthTotal = 0.0;
				let size = 0;
				for (let index in data.dimensionScores) {
					let item = data.dimensionScores[index];
				
					item.month = item.month / 10;
					item.dqFraction = item.dq / 10;
				
					if (item.dimensionName === '交流互动警示行为') {
						item.dimensionNamev2 = "警示\n行为";
					} else if (item.dimensionName === '精细动作') {
						item.dimensionNamev2 = '精细\n动作'
					} else if (item.dimensionName === '适应能力') {
						item.dimensionNamev2 = '适应\n能力'
					} else if (item.dimensionName === '社会行为') {
						item.dimensionNamev2 = '社会\n行为'
					} else if (item.dimensionName === '警示行为') {
						item.dimensionNamev2 = '警示\n行为'
					} else {
						item.dimensionNamev2 = item.dimensionName;
					}
				
					if (item.dimensionName != '交流互动警示行为') {
						dqTotal += item.dqFraction;
						monthTotal += item.month;
						size += 1;
					}
				}
				
				let wisdomAge = monthTotal / size;
				let dqFraction = dqTotal / size;
				
				let level = '';
				if (dqFraction >= 130) {
					level = '超常';
				} else if (dqFraction >= 115) {
					level = '优秀';
				} else if (dqFraction >= 85) {
					level = '正常';
				} else if (dqFraction >= 70) {
					level = '落后';
				} else {
					level = '偏低';
				}
				
				let suggest = '';
				for (let index in data.failedQuestions) {
					let item = data.failedQuestions[index];
					if (item.month <= month) {
						suggest = suggest + '\n' + item.title + "\n" + item.advise + "\n";
					}
				}
				
				this.warningConclusion = data.warningConclusion;
				this.normalConclusion = data.normalConclusion;
				this.assessDetailEntity = data;
				this.wisdomAge = wisdomAge;
				this.dqFraction = dqFraction;
				this.level = level;
				this.suggest = suggest;
				this.hasData = true;
				
				this.assessDetailEntity = data;
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: $app-page-bg-color;
	}
	
	.container{
		display: flex;
		flex-direction: column;
	}
	
	.item-view{
		background-color: #FFFFFF;
		padding: 30rpx;
		margin-bottom: 20rpx;
	}
	
	.item-view-grey{
		background-color: $app-page-bg-color;
	}
	
	.ling-view{
		height: 20rpx;
	}

</style>
